package com.chezai.client.widget;

import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.chezai.client.inter.OnTimerListener;

import java.util.concurrent.TimeUnit;

/**
 * 计时textView
 * Created by likunyang on 2017/9/21.
 */

public class TimerTextView extends AppCompatTextView implements TimerManager.OnTimerManagerListener {
    public static final int EVENT_TEN_SECONDS = 1;//每10s事件
    public static final int EVENT_TWENTY_SECONDS = 2;//每20s事件
    public static final int EVENT_ONE_MINUTES = 3;//每一分钟事件
    public static final int EVENT_EIGHT_MINUTES = 5;//每八分钟事件
    private static final int CUSTOM_MINUTE = 450;//自定义分钟事件
    private long mTime = 0;
    private OnTimerListener mTimerListener;
    private TimerManager tm;

    public TimerTextView(Context context) {
        this(context, null);
    }

    public TimerTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        tm = TimerManager.getInstance();
        tm.setOnTimerListener(this);
    }

    /**
     * 开始计时
     */
    public void start() {
        if (tm.isStart()) return;
        tm.start();
    }

    /**
     * 停止计时
     */
    public void stop() {
        if (!tm.isStart()) return;
        tm.stop();
    }


    /**
     * 格式化计时 hh:mm:ss
     */
    private void formatSecond() {
        long hours = TimeUnit.SECONDS.toHours(mTime);
        long minutes = TimeUnit.SECONDS.toMinutes(mTime - hours * 60 * 60);
        long second = mTime - (TimeUnit.HOURS.toSeconds(hours) + TimeUnit.MINUTES.toSeconds(minutes));
        setText(String.format("%1$02d：%2$02d：%3$02d", hours, minutes, second));
    }

    /**
     * 设置计时监听
     * 如果想监听到所有监听，需要在start 之前 调用此方法
     *
     * @param listener
     */
    public void setOnTimerListener(OnTimerListener listener) {
        if (mTimerListener != null && mTimerListener == listener) return;
        this.mTimerListener = listener;
    }

    /**
     * 开始时间  单位秒
     *
     * @param startTime 开始秒数
     */
    public void setStartTime(@IntRange(from = 0) long startTime, TimeUnit timeUnit) {
        this.mTime = timeUnit.toSeconds(startTime);
        formatSecond();
    }

    /**
     * 获取计时时间
     *
     * @param timeUnit TimeUnit
     * @return 获取当前已计时时长
     */
    public long getTime(TimeUnit timeUnit) {
        return timeUnit.convert(mTime, timeUnit);
    }

    /**
     * 是否开始计时
     *
     * @return true 已开始，false 未开始
     */
    public boolean isStarting() {
        return tm.isStart();
    }

    public void resume() {
        start();
    }

    /**
     * 分发计时事件
     * 1s,10s,1m,15m
     */
    private void dispatchTimerEvent() {
        if (mTimerListener == null) return;
        mTimerListener.onTick(this, mTime);
        if (mTime % 60 == 0) {
            mTimerListener.onTimerEvent(this, EVENT_ONE_MINUTES);
        }
        if (mTime % 10 == 0) {
            mTimerListener.onTimerEvent(this, EVENT_TEN_SECONDS);
        }
        if (mTime % 20 == 0) {
            mTimerListener.onTimerEvent(this, EVENT_TWENTY_SECONDS);
        }
        if (mTime == CUSTOM_MINUTE) {
            mTimerListener.onTimerEvent(this, EVENT_EIGHT_MINUTES);
        }
    }

    @Override
    public void onTick(TimerManager timerManager) {
        mTime++;
        formatSecond();
        dispatchTimerEvent();
    }

    @Override
    public void onTimerStart(TimerManager timerManager) {
        if (mTimerListener == null) return;
        mTimerListener.onTimerStart(this, mTime);
    }

    @Override
    public void onTimerStop(TimerManager timerManager) {
        if (mTimerListener == null) return;
        mTimerListener.onTimerStop(this, mTime);
    }

    public void unRegisterTimerListner() {
        if (mTimerListener != null) {
            mTimerListener = null;
        }
        tm.unRegisterTimerListener();
    }
}
